<template>
  <div class="my-box">
    <!-- 未登陆头部 -->
    <div v-if="!user" @click="$router.push('/login')" class="not-login header">
      <img src="~@/assets/mobile.png" />
      <span>登陆 / 注册</span>
    </div>
    <!-- 已登陆头部 -->
    <div v-else class="header login">
      <div class="userInfo">
        <div class="left">
          <van-image
            round
            class="avatar"
            :src="userInfo.photo"
          />
          <span class="user_name">{{userInfo.name}}</span>
        </div>
        <van-button to="/user" size='mini' round type="default">编辑资料</van-button>
      </div>
      <div class="userData">
        <div class="data_item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data_item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid :column-num="2">
      <van-grid-item>
        <i slot='icon' class='toutiao toutiao-shoucang'></i>
        <span slot='text' class='text'>收 藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot='icon' class='toutiao toutiao-lishi'></i>
        <span slot='text' class='text'>历 史</span>
      </van-grid-item>
    </van-grid>
    <!-- cell 结构 -->
    <div class="cell-box1">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <div class="logout">
      <van-cell @click='onLogout' v-if="user" title="退出" />
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  name: 'my',
  data () {
    return {
      userInfo: ''
    }
  },

  created () {
    this.loadUserInfo()
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    onLogout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认'
      })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(_ => {})
    },
    async loadUserInfo () {
      if (!this.user) return
      try {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
      } catch (err) {
        this.$toast.fail('用户认证失败!')
      }
    }
  }
}
</script>

<style scoped lang='less'>
  .my-box {
    .header {
      width: 750px;
      height: 401px;
      background-image: url(~@/assets/banner.png);
      background-size: cover;
    }
    // 未登陆头部
    .not-login {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      img {
        width: 132px;
        height: 132px;
      }
      span {
        font-size: 28px;
        color: #fff;
        margin-top: 15px;
      }
    }
    // 已登陆头部
    .login {
      overflow: hidden;
      .userInfo {
        height: 160px;
        margin-top: 80px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 32px;
        .left {
          display: flex;
          align-items: center;
          .avatar {
            width: 132px;
            height: 132px;
          }
          .user_name {
            font-size: 30px;
            margin-left: 22px;
            color: #fff
          }
        }
      }
      .userData {
        height: 161px;
        display: flex;
        .data_item {
          flex: 1;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          color: #fff;
          span:nth-child(1) {
            font-size: 36px;
            margin-bottom: 18px;
          }
          span:nth-child(2) {
            font-size: 23px;
          }
        }
      }
    }
    // 宫格导航
    .van-grid {
      .toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
        color: #eb5253
      }
      .toutiao-lishi {
        color: #ff9d1d
      }
      .text {
        font-size: 28px;
        margin-top: 16px;
      }
    }
    // cell 结构
    .cell-box1 {
      margin: 10px 0;
    }
    .logout {
      text-align: center;
      .van-cell {
        color: #EB5253;
      }
    }
  }
</style>
